<?php

/**
 * 权限控制器
 * @author yanghao <yh38615890@sina.cn>
 * @date 07-04-12
 * @copyright (c) gandianli
 */

namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Db;
use think\Loader;
use think\exception\HttpException;
use com\gdl\newagk\interfaces\LabelsServiceClient;
use com\gdl\newagk\dataobject\labels\LabelsRequestDTO; 
use com\gdl\newagk\dataobject\labels\LabelsDTO;

class Labels extends Base
{

	public function index(){
	}

	//标签列表
	public function labeList(){
		$reg = $this->request->param('reg') ? getStr($this->request->param('reg'), 'string') : '';
		$paperid = $this->request->param('paperid') ? getStr($this->request->param('paperid'), 'int') : 0;
		/*
		$reg = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
		$paperid = 1;
		*/
		if($reg==''){
			$data = ['msg'=>'缺少参数','code'=>'500'];
			return json_encode($data);
		}

		
			try {
				\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
				$api = new LabelsServiceClient(NULL);
				\ApiClient::build($api);
				$engprojectDo = new LabelsDTO();
				$engprojectDo->reg = $reg;
				$engprojectDo->id = $paperid;
				$result = $api->labeList($engprojectDo);
				if($result->code){
					$data = ['msg'=>'成功','code'=>'200','data'=>$result->data];
				} else {
					$data = ['msg'=>'失败','code'=>'400','data'=>[]];
				}
			} catch (Exception $ex) {
				$data = ['msg'=>'失败','code'=>'400','data'=>[]];
			}

			return json_encode($data);

	}
	
	public function tagInfo(){
		$reg = $this->request->param('reg') ? getStr($this->request->param('reg'), 'string') : '';
		$id = $this->request->param('id') ? getStr($this->request->param('id'), 'string') : '';

		if($id=='' || $reg=='') {
			$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}

		try {
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
			$api = new LabelsServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new LabelsDTO();
			$engprojectDo->reg = $reg;
			$engprojectDo->field = $id;
			$result = $api->details($engprojectDo);
	
			if($result->code){
				$result->data[0]['tag'] = json_decode($result->data[0]['tag'],true);
				$result->data[0]['tagSelect'] = json_decode($result->data[0]['tagSelect'],true);
				$data = ['msg'=>'成功','code'=>'200','data'=>$result->data[0]];
			} else {
				$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
			}
		} catch (Exception $ex) {
			$data = ['msg'=>'失败','code'=>'400','data'=>[]];
		}
		return json_encode($data);

	}
	
	

	//添加标签
	public function add(){
		$filename = $this->request->post();
				/*$filename['title'] = '试卷分类';
				$filename['tag'] = [['id'=>0,'name'=>'安全知识'],'高压高电','设备设施'];
				$filename['reg'] = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
				$filename['ords'] = 1;
				*/
				 //测试用
		if (empty($filename['title']) || empty($filename['reg'])) {
			$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}

		try {
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
			$api = new LabelsServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new LabelsDTO();
			$engprojectDo->title = getStr($filename['title'], 'string');
			$engprojectDo->filename = $filename['tag'];
			$engprojectDo->reg = getStr($filename['reg'], 'string');
			$engprojectDo->id = getStr($filename['ords'], 'int');
			$result = $api->add($engprojectDo);

			if($result->code){
				$data = ['msg'=>'成功','code'=>'200','data'=>[]];
			} else {
				$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
			}
		} catch (Exception $ex) {
			$data = ['msg'=>'失败','code'=>'400','data'=>[]];
		}
		return json_encode($data);
	}
	
	//查询标签详情
	public function labelsDet(){
		
		$reg = $this->request->param('reg') ? getStr($this->request->param('reg'), 'string') : '';
		$id = $this->request->param('id') ? getStr($this->request->param('id'), 'int') : 0;
		/*
		$reg = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
		$id = 1;
		*/
		if(!$id || $reg=='') {
			$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}

		try {
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
			$api = new LabelsServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new LabelsDTO();
			$engprojectDo->reg = $reg;
			$engprojectDo->id = $id;
			$result = $api->details($engprojectDo);
	
			if($result->code){
				$result->data[0]['tag'] = json_decode($result->data[0]['tag'],true);
				$data = ['msg'=>'成功','code'=>'200','data'=>$result->data[0]];
			} else {
				$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
			}
		} catch (Exception $ex) {
			$data = ['msg'=>'失败','code'=>'400','data'=>[]];
		}
		return json_encode($data);
	}
	//修改分类
	public function edit(){
		$filename = $this->request->post();
			/*	$filename['title'] = '试卷分类2';
				$filename['tag'] = [['id'=>0,'name'=>'安全知识2'],['id'=>2,'name'=>'高压变电'],['id'=>0,'name'=>'设备设施2']];
				$filename['reg'] = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';
				$filename['ords'] = 2;
				$filename['id'] = 1;
			*/

				 //测试用
		$filename['id'] =$filename['id'] ? getStr($filename['id'], 'int') : 0;
		
		
		if (empty($filename['tag']) || empty($filename['reg']) || !$filename['id']) {
			$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}

		try {
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
			$api = new LabelsServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new LabelsDTO();
			$engprojectDo->title = getStr($filename['tag'], 'string');
			$engprojectDo->filename = $filename['tagSelect'];
			$engprojectDo->reg = getStr($filename['reg'], 'string');
			$engprojectDo->code = getStr($filename['sort'], 'int');
			$engprojectDo->id = $filename['id'];
			$result = $api->edit($engprojectDo);

			if($result->code){
				$data = ['msg'=>'成功','code'=>'200','data'=>[]];
			} else {
				$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
			}
		} catch (Exception $ex) {
			$data = ['msg'=>'失败','code'=>'400','data'=>[]];
		}
		return json_encode($data);
	}
	
	//删除标签
	public function delOut(){

			/*	$filename['reg'] = 'j4timvso_54lkl9x3flob595f327ba4cb3gdl_test';

				$filename['id'] = 1;
				*/
				 //测试用
				 
		$filename['reg'] = $this->request->param('reg') ? getStr($this->request->param('reg'), 'string') : '';
		$filename['id'] = $this->request->param('id') ? getStr($this->request->param('id'), 'int') : 0;

		if ($filename['reg']=='' || !$filename['id']) {
			$data = ['msg'=>'缺少必要参数','code'=>'500','data'=>[]];
			return json_encode($data);
		}

		try {
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
			$api = new LabelsServiceClient(NULL);
			\ApiClient::build($api);
			$engprojectDo = new LabelsDTO();
			$engprojectDo->reg = getStr($filename['reg'], 'string');
			$engprojectDo->id = $filename['id'];

			$result = $api->delOut($engprojectDo);
			if($result->code){
				$data = ['msg'=>'成功','code'=>'200','data'=>[]];
			} else {
				$data = ['msg'=>$result->message,'code'=>'400','data'=>[]];
			}
		} catch (Exception $ex) {
			$data = ['msg'=>'失败','code'=>'400','data'=>[]];
		}
		return json_encode($data);
	}

}
